package com.doyens.auth.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.auth.service.SysRoleService;
import com.doyens.common.result.Result;
import com.doyens.execption.DoyensException;
import com.doyens.model.system.SysRole;
import com.doyens.vo.system.AssginRoleVo;
import com.doyens.vo.system.SysRoleQueryVo;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.digester.Rules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired //自动按照类型注入
    private SysRoleService sysRoleService;
//   给用户分配角色
    @ApiOperation("根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
     public Result toAssign(@PathVariable int userId){
         Map<String,Object> roleMap=sysRoleService.findRole(userId);
         return Result.ok(roleMap);
     }
     //根据用户分配角色
    @ApiOperation("根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
          sysRoleService.doAssign(assginRoleVo);
          return Result.ok("分配成功");
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/save")
    public Result<String> save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok("添加成功");
    }

    //分页查询所有角色
    @ApiOperation("分页查询所有角色")
      @GetMapping("/{page}/{pageSize}")
      public Result<Page<SysRole>> page1(@PathVariable int page,@PathVariable int pageSize, SysRoleQueryVo sysRole){
        Page<SysRole> sysRolePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<SysRole> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(sysRole.getRoleName()),SysRole::getRoleName,sysRole.getRoleName());
        Page<SysRole> page1 = sysRoleService.page(sysRolePage, wrapper);
        return Result.ok(page1).message("分页查询");
    }
    @ApiOperation("查看所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll(){
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result<String> update(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.ok("修改成功");
    }
    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{id}")
    public Result<String> del(@PathVariable int id){
        sysRoleService.removeById(id);
//        int a = 10/0;
        return Result.ok("删除成功");
    }
    @ApiOperation("批量删除角色")
    @PostMapping("/delMany")
    public Result<String> delMany(@RequestBody List<Integer> list){
        sysRoleService.removeByIds(list);
        return Result.ok("批量删除成功");
    }
    @ApiOperation("根据id查询角色")
    @GetMapping("/getById/{id}")
    public Result<SysRole> getById(@PathVariable int id){
//        if(id<0){
//             throw new DoyensException(201,"id不能为负数");
//        }
        SysRole byId = sysRoleService.getById(id);
//        int a=5/0;
        return Result.ok(byId);
    }
}
